package com.netease.research.service.impl;

import com.netease.research.Vo.AccountVo;
import com.netease.research.common.ServerResponse;
import com.netease.research.dao.AccountMapper;
import com.netease.research.dao.CartMapper;
import com.netease.research.dao.ProductMapper;
import com.netease.research.pojo.Account;
import com.netease.research.pojo.Cart;
import com.netease.research.pojo.Product;
import com.netease.research.service.IAccountService;
import com.netease.research.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("iAccountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public ServerResponse<List<AccountVo>> createOrder(Integer userId) {
        List<Cart> cartList = cartMapper.selectByUserId(userId);
        List<AccountVo> accountVoList = new ArrayList<>();
        for (Cart cart : cartList) {
            int quantity = cart.getQuantity();
            int productId = cart.getProductId();
            Product product = productMapper.selectByPrimaryKey(productId);
            BigDecimal price = product.getPrice();
            BigDecimal totalPrice = price.multiply(new BigDecimal(quantity)); //该商品总价
            Date buyTime = new Date(System.currentTimeMillis());
            Account item = new Account();
            item.setBuyTime(buyTime);
            item.setUserId(userId);
            item.setProductId(cart.getProductId());
            item.setQuantity(cart.getQuantity());
            item.setPrice(price);
            item.setTotalPrice(totalPrice);
            item.setTitle(product.getTitle());
            item.setPicUrl(product.getPicUrl());
            accountMapper.insertReturnId(item);//返回自增主键
            accountVoList.add(assembleAccountVo(item));
//          商品下架
            productMapper.updateSaleStatus(productId);

        }
//        清空购物车
        cartMapper.deleteByUserId(userId);
        return ServerResponse.createBySuccess(accountVoList);
    }

    private AccountVo assembleAccountVo(Account account){
        AccountVo vo = new AccountVo();
        vo.setId(account.getId());
        vo.setUserId(account.getUserId());
        vo.setProductId(account.getProductId());
        vo.setBuyTime(DateTimeUtil.dateToStr(account.getBuyTime()));//Date转换为string
        vo.setQuantity(account.getQuantity());
        vo.setPrice(account.getPrice());
        vo.setTotalPrice(account.getTotalPrice());
        vo.setTitle(account.getTitle());
        vo.setPicUrl(account.getPicUrl());
        return vo;
    }

    @Override
    public ServerResponse<List<AccountVo>> listAll(Integer userId) {
        List<Account> accountList = accountMapper.selectByUserId(userId);
        List<AccountVo> accountVoList = new ArrayList<>();
        for(Account item : accountList){
            AccountVo vo = assembleAccountVo(item);
            accountVoList.add(vo);
        }
        return ServerResponse.createBySuccess(accountVoList);
    }
}
